/// <license>TUSTENA PUBLIC LICENSE v1.0</license>
/// <copyright>
/// Portions Copyright (c) 2003-2006 Digita S.r.l. All Rights Reserved.
///
/// Tustena CRM is a trademark of:
/// Digita S.r.l.
/// Viale Enrico Fermi 14/z
/// 31011 Asolo (Italy)
/// Tel. +39-0423-951251
/// Mail. info@digita.it
///
/// This file contains Original Code and/or Modifications of Original Code
/// as defined in and that are subject to the Tustena Public Source License
/// Version 1.0 (the 'License'). You may not use this file except in
/// compliance with the License. Please obtain a copy of the License at
/// http://www.tustena.com/TPL/ and read it before using this
// file.
///
/// The Original Code and all software distributed under the License are
/// distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
/// EXPRESS OR IMPLIED, AND DIGITA S.R.L. HEREBY DISCLAIMS ALL SUCH WARRANTIES,
/// INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
/// Please see the License for the specific language governing rights and
/// limitations under the License.
///
/// YOU MAY NOT REMOVE OR ALTER THIS COPYRIGHT NOTICE!
/// </copyright>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Digita.Tustena.Database;
using System.Collections;

namespace Digita.Tustena.Common
{
    public class Recent
    {
        public static DataTable LoadRecentItems(int userId)
        {
            DataSet ds = DatabaseConnection.CreateDataset("SELECT DISTINCT TYPE, TEXT, RECID FROM RECENT WHERE USERID=" + userId);
            return ds.Tables[0];
        }

        public static void AddItem(int userId, string text, int record, RecentType type, RecentMode mode)
        {
            DatabaseConnection.DoCommand(string.Format("INSERT INTO RECENT (USERID, RECID, TYPE, TEXT) VALUES({0}, {1}, {2}, N'{3}')", userId, record, (byte)type, text));
            ClearOldItems(userId,10);
        }

        private static void ClearOldItems(int userId, int limit)
        {
            DatabaseConnection.DoCommand(string.Format("DELETE FROM RECENT WHERE ID NOT IN (SELECT TOP {1} ID FROM RECENT WHERE USERID={0} ORDER BY LASTDATE DESC) AND USERID={0}", userId, limit));
        }

    }
    public enum RecentType : byte { Company, Contact, Lead }
    public enum RecentMode : byte { New, Read, Write }
}
